Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
@sindresorhus/transliterate
Advanced tools
Convert Unicode characters to Latin characters using transliteration
@sindresorhus/transliterate is an npm package that provides functionality to transliterate Unicode strings into ASCII. This is particularly useful for converting non-Latin scripts into a Latin-based alphabet, making the text easier to read and process in various applications.
Basic Transliteration
This feature allows you to transliterate a given Unicode string into its ASCII representation. In this example, the Chinese characters '你好' are transliterated to 'Ni Hao'.
const transliterate = require('@sindresorhus/transliterate');
console.log(transliterate('你好')); // Output: 'Ni Hao'
Custom Replacement Map
This feature allows you to provide a custom replacement map for specific characters. In this example, the custom map is used to transliterate '你好' to 'Nǐ Hǎo' instead of the default 'Ni Hao'.
const transliterate = require('@sindresorhus/transliterate');
const customMap = { '你': 'Nǐ', '好': 'Hǎo' };
console.log(transliterate('你好', { customReplacements: customMap })); // Output: 'Nǐ Hǎo'
Slugify
This feature allows you to create URL-friendly slugs from Unicode strings. In this example, the string '你好, 世界!' is transliterated and slugified to 'ni-hao-shi-jie'.
const transliterate = require('@sindresorhus/transliterate');
console.log(transliterate.slugify('你好, 世界!')); // Output: 'ni-hao-shi-jie'
The 'transliteration' package provides similar functionality for converting Unicode strings to ASCII. It also supports custom replacement maps and slugification. Compared to @sindresorhus/transliterate, it offers a more extensive set of default transliterations and additional configuration options.
The 'unidecode' package is another alternative for transliterating Unicode strings to ASCII. It is based on the Python Unidecode library and provides a straightforward way to convert non-Latin scripts. While it is effective, it may not offer as much customization as @sindresorhus/transliterate.
The 'slugify' package focuses on creating URL-friendly slugs from strings, including Unicode strings. It provides a range of options for customization and is highly performant. While it specializes in slug creation, it may not offer the same level of general transliteration functionality as @sindresorhus/transliterate.
Convert Unicode characters to Latin characters using transliteration
Can be useful for slugification purposes and other times you cannot use Unicode.
$ npm install @sindresorhus/transliterate
import transliterate from '@sindresorhus/transliterate';
transliterate('Fußgängerübergänge');
//=> 'Fussgaengeruebergaenge'
transliterate('Я люблю единорогов');
//=> 'Ya lyublyu edinorogov'
transliterate('أنا أحب حيدات');
//=> 'ana ahb hydat'
transliterate('tôi yêu những chú kỳ lân');
//=> 'toi yeu nhung chu ky lan'
Type: string
String to transliterate.
Type: object
Type: Array<string[]>
Default: []
Add your own custom replacements.
The replacements are run on the original string before any other transformations.
This only overrides a default replacement if you set an item with the same key.
import transliterate from '@sindresorhus/transliterate';
transliterate('Я люблю единорогов', {
customReplacements: [
['единорогов', '🦄']
]
})
//=> 'Ya lyublyu 🦄'
Most major languages are supported.
This includes special handling for:
However, Chinese is currently not supported.
FAQs
Convert Unicode characters to Latin characters using transliteration
The npm package @sindresorhus/transliterate receives a total of 913,186 weekly downloads. As such, @sindresorhus/transliterate popularity was classified as popular.
We found that @sindresorhus/transliterate demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.